package m.system.listener;

import m.common.action.Action;
import m.common.action.ActionRequest;
import m.common.model.*;
import m.common.model.config.ModelConfig;
import m.common.model.util.ModelUtil;
import m.system.exception.MException;
import m.system.task.TaskUtil;
import m.system.util.*;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SystemInit {
	/**
	 * 设置数据库名称
	 *  tableSchema
	 */
	public static void setTableSchema(String tableSchema){
		ModelConfig.setTableSchema(tableSchema);
	}
	/**
	 * 初始化数据表结构缓存
	 *  modelPage
	 */
	@SuppressWarnings("unchecked")
	public static void initModel(String modelPack) throws ClassNotFoundException {
		SystemUtil.printlnIf(true,"初始化表结构>>> ");
		if(!StringUtil.isSpace(modelPack)) {
			String[] packs=StringUtil.toArrayDedup(modelPack,",");
			List<Class<Model>> lastInit= new ArrayList<>();
			for (String pack : packs) {
				if (StringUtil.isSpace(pack)) continue;
				List<String> modelNames = ClassUtil.getAllQualifiedName4Class(pack);
				for (String modelName : modelNames) {
					Class<?> cc = Class.forName(modelName);
					if (Model.class.isAssignableFrom(cc)) {
						Class<Model> clazz = (Class<Model>) cc;
						TableMeta tm = AnnotationUtil.getAnnotation4Class(TableMeta.class, clazz);
						if (null != tm) {
							if (tm.isView()) {
								lastInit.add(clazz);
							} else {
								initModel(clazz, tm);
							}
						}
					}
				}
			}
			for(Class<Model> clazz : lastInit){
				TableMeta tm=AnnotationUtil.getAnnotation4Class(TableMeta.class,clazz);
				initModel(clazz,tm);
			}
		}
		SystemUtil.printlnIf(true,"初始化表结构完成<<<");
	}
	private static <T extends Model> void initModel(Class<T> clazz,TableMeta tm){
		if(null!=tm) {
			Map<String,FieldMeta> fieldMap=AnnotationUtil.getAnnotationMap4Field(FieldMeta.class,clazz);
			Map<String,LinkTableMeta> linkTableMap=AnnotationUtil.getAnnotationMap4Field(LinkTableMeta.class,clazz);
			Map<String,QueryFieldMeta> queryFieldMap=AnnotationUtil.getAnnotationMap4Field(QueryFieldMeta.class,clazz);
			ModelConfig.fillModelInfo(clazz, tm, fieldMap, linkTableMap, queryFieldMap);
		}
	}
	/**
	 * 初始化数据库表结构
	 */
	public static void initModelTable() throws MException, SQLException {
		for(Class<? extends Model> clazz : ModelConfig.getTableList()){
			ModelUtil.initModelTable(clazz);
		}
	}
	public static void initTask(String taskPack) throws ClassNotFoundException {
		if(StringUtil.isSpace(taskPack)) return ;
		String[] packs=StringUtil.toArrayDedup(taskPack,",");
		List<String> list=new ArrayList<>();
		for (String pack : packs) {
			if (StringUtil.isSpace(pack)) continue;
			list.addAll(ClassUtil.getAllQualifiedName4Class(pack));
		}
		TaskUtil.initTask(list);
	}
	@SuppressWarnings("unchecked")
	public static void initAction(String actionPack) throws ClassNotFoundException {
		if(StringUtil.isSpace(actionPack)) return ;
		String[] packs=StringUtil.toArrayDedup(actionPack,",");
		for (String pack : packs) {
			if (StringUtil.isSpace(pack)) continue;
			List<String> modelNames = ClassUtil.getAllQualifiedName4Class(pack);
			for (String modelName : modelNames) {
				Class<?> clazz = Class.forName(modelName);
				if (Action.class.isAssignableFrom(clazz)) {
					ActionRequest meta = AnnotationUtil.getAnnotation4Class(ActionRequest.class, clazz);
					if (null != meta) {
						ActionUtil.init(meta,(Class<Action>) clazz);
					}
				}
			}
		}
	}
}
